package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.GlobalRomingUserAdapter;
import com.sam.im.heiqun.uis.beans.GlobalRoamingBeans;
import com.sam.im.heiqun.uis.beans.GlobalRoamingUserBean;
import com.sam.im.heiqun.utils.MyDialog;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.RefreshAndLoad.PullToRefreshLayout;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/9/8.
 */

public class GlobalRoamingUserListActivity extends BaseSwipeBackActivity {

    @BindView(R.id.user_refresh)
    PullToRefreshLayout plContent;
    @BindView(R.id.lv_content)
    RecyclerView lv_content;
    @BindView(R.id.right)
    ImageView right;

    GlobalRoamingBeans globalRoamingBeans ;
    private GlobalRomingUserAdapter mAdapter ;
    List<GlobalRoamingUserBean> list ;

    private double mylongitude = 0;
    private double mylatitude = 0;
    private double longitude = 0;
    private double latitude = 0;

    int pageNo = 0 ;
    PGService mPgService;
    String sex = "" ;

    public static void start(Activity activity, GlobalRoamingBeans globalRoamingBeans, double mylongitude, double mylatitude
            , double longitude, double latitude){
        Intent intent = new Intent(activity, GlobalRoamingUserListActivity.class);
        intent.putExtra("globalRoamingBeans",globalRoamingBeans);
        intent.putExtra("mylongitude",mylongitude);
        intent.putExtra("mylatitude",mylatitude);
        intent.putExtra("longitude",longitude);
        intent.putExtra("latitude",latitude);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.near_person);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();
        right.setBackgroundResource(R.drawable.more_max);
        right.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        globalRoamingBeans = (GlobalRoamingBeans) getIntent().getSerializableExtra("globalRoamingBeans");
        mylongitude = getIntent().getDoubleExtra("mylongitude", 0);
        mylatitude = getIntent().getDoubleExtra("mylatitude", 0);
        longitude = getIntent().getDoubleExtra("longitude", 0);
        latitude = getIntent().getDoubleExtra("latitude", 0);
        if (null!=globalRoamingBeans && null!=globalRoamingBeans.getList())list.addAll(globalRoamingBeans.getList());
        plContent.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onPullDownToRefresh() {
                pageNo = 0 ;
                qqmy();
            }
            @Override
            public void onPullUpToRefresh() {
                pageNo ++ ;
                qqmy();
            }
        });
//        plContent.autoRefresh();

        lv_content.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mAdapter = new GlobalRomingUserAdapter(GlobalRoamingUserListActivity.this,list,mylongitude,mylatitude);
        lv_content.setAdapter(mAdapter);
        mAdapter.setOnClickListener(new GlobalRomingUserAdapter.OnClickListener() {
            @Override
            public void OnClick(GlobalRoamingUserBean noticeBean, int type) {
                Intent intent = new Intent(GlobalRoamingUserListActivity.this, FriendDetailActivity.class);
                intent.putExtra("type",0);
                intent.putExtra("id",Long.parseLong(noticeBean.getId()));
                startActivity(intent);
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_global_roaming_userlist;
    }

    /**
     * 请求漫游信息
     */
    private void qqmy() {
        mPgService.findNearby(ToolsUtils.getMyUserId(), ""+latitude, ""+longitude, sex, ""+pageNo).subscribe(new AbsAPICallback<GlobalRoamingBeans>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast(getResources().getString(R.string.get_data_fail));
            }

            @Override
            public void onNext(GlobalRoamingBeans mglobalRoamingBeans) {
//
                if (null!=mglobalRoamingBeans){
                    if (0 == pageNo){
                        list.clear();
                        if (null!=mglobalRoamingBeans.getList() && mglobalRoamingBeans.getList().size() > 0){
                            list.addAll(mglobalRoamingBeans.getList());
                        }
                        mAdapter.notifyDataSetChanged();
                        plContent.setPullUpEnable(true);
                    }else {
                        if (pageNo >= Integer.parseInt(mglobalRoamingBeans.getPageSize())){
                            plContent.setPullUpEnable(false);
                        }
                        if (null!=mglobalRoamingBeans.getList() && mglobalRoamingBeans.getList().size() > 0){
                            int start = list.size();
                            list.addAll(mglobalRoamingBeans.getList());
                            mAdapter.notifyItemRangeChanged(start, mglobalRoamingBeans.getList().size());
                        }else {
                            plContent.setPullUpEnable(false);
                            showToast(getResources().getString(R.string.get_data_all));
                        }
                    }
                    plContent.stopLoading();
                }
            }
        });
    }

    @OnClick({ R.id.right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back :
                scrollToFinishActivity();
                break;
            case R.id.right:
                String [] items = new String[]{getResources().getString(R.string.women_look), getResources().getString(R.string.man_look), getResources().getString(R.string.all_look)};
                Dialog dialog = MyDialog.ShowDialog(GlobalRoamingUserListActivity.this, "", items, new MyDialog.DialogItemClickListener() {
                    @Override
                    public void confirm(String result) {
                        if (getResources().getString(R.string.women_look).equals(result)){
                            sex = getResources().getString(R.string.women);
                        }else if (getResources().getString(R.string.man_look).equals(result)){
                            sex = getResources().getString(R.string.man);
                        }else if (getResources().getString(R.string.all_look).equals(result)){
                            sex = "";
                        }
                        pageNo = 0 ;
                        qqmy();
                    }
                });
                dialog.show();
                break;
        }
    }

}
